#ifndef INTERFAC
#define INTERFAC

#include <glui.h>
#include <GL/glut.h>
#include <stdio.h>

#include "vetores.h"
#include "fmmtcir.h"
#include "fmmtplan.h"
#include "about.h"

extern sistema ms;
extern long int cmd_steppsim;
bool ok=true, step=false;

extern int janvetores;
extern bool showvetores, firstvetores;
extern void vetoresKeyboard(unsigned char key, int x, int y);
extern int janfmmtcir;
extern bool showfmmtcir, firstfmmtcir;
extern int janabout;
extern bool showabout, firstabout;


GLUI *interf;						// janela principal
GLUI_Spinner *spine, *spinpt;		// spinners de E e PT
GLUI_StaticText *titulo;			// Label 
GLUI_Rollout *sim;					// panel para armazenar os botoes da simulacao
GLUI_Rollout *graficos;			// controle de exibicao dos graficos
GLUI_Rollout *opcoes;
GLUI_Button *b1,*b2,*b3,*b4,*b5;
GLUI_Button *b6,*b7,*b8,*b9,*b0;
GLUI_Button *cplay, *splay, *stop;	// Botoes de Continuous Play, Step Play e STOP
GLUI_Button *vet,*dcir,*dplan;		// botoes para exibicao dos diagramas
GLUI_Button *about;
GLUI_Button *quit;					// botao de saida...

long int localie=IEXC_NOM, localpt=(long int)(PT_NOM/1000); //valores iniciais para os controles de E e PT

void calcSistema(void)
{
	double tempi,tempe,m;int p;
	if (ok || step)
	{
		ms.go(cmd_steppsim);
		estator.setphase(-r2d(ms.fi));
		tempi=ms.I.abs();
		tempe=ms.e.abs();
		
		estator.setampl(15*tempi/1000);
		rotor.setampl(15*tempe/(1000*ms.xs));
		rotor.setphase(r2d(ms.delta+3.1415/2));
		calcres(&estator, &rotor, &m, &p);
		resul.setampl((float)m);
		resul.setphase(p);
		
		grafdelta.addpoint(ms.ttotal, r2d(ms.delta));
		grafcosfi.addpoint(ms.ttotal, cos(ms.fi));
		grafi.addpoint(ms.ttotal, tempi);
		grafwr.addpoint(ms.ttotal, ms.wr*60/(2*pi()));
		grafpt.addpoint(ms.ttotal, ms.pt/SBASE);
		grafe.addpoint(ms.ttotal, tempe);
		grafs.addpoint(ms.ttotal, ms.s.abs()/SBASE);
		grafq.addpoint(ms.ttotal, ms.q/SBASE);
		grafp.addpoint(ms.ttotal, ms.p/SBASE);
		step=false;

		controleRedisplay();
	}
}


void changeControl(int id)
{
	interf->sync_live();

		ms.setIEXC((double)localie);
		ms.setPT((double)localpt*1000);
}

void pushControl(int id)
{
	switch(id)
	{
	case 1:
		ok=true;
		break;
	case 2:
		step=true;
		break;
	case 3:
		ok=false;
		break;
	case 5:			//exibe o diagrama vetorial
		if(firstvetores)
		{
			vetoresCreate();
			firstvetores=false;
		}
		showvetores=!showvetores;
		if (showvetores)
		{
			glutSetWindow(janvetores);
			glutShowWindow();
			glutPostRedisplay();
		}
		else
		{
			glutSetWindow(janvetores);
			glutHideWindow();
		}
		break;
	case 6:			//exibe o diagrama de fmmt do estator e do rotor
		if(firstfmmtcir)
		{
			fmmtcirCreate();
			firstfmmtcir=false;
		}
		showfmmtcir=!showfmmtcir;
		if (showfmmtcir)
		{
			glutSetWindow(janfmmtcir);
			glutShowWindow();
			glutPostRedisplay();
		}
		else
		{
			glutSetWindow(janfmmtcir);
			glutHideWindow();
		}
		break;
	case 7:			//exibe o diagrama planificado de fmmts
		if(firstfmmtplan)
		{
			fmmtplanCreate();
			firstfmmtplan=false;
		}
		showfmmtplan=!showfmmtplan;
		if (showfmmtplan)
		{
			glutSetWindow(janfmmtplan);
			glutShowWindow();
			glutPostRedisplay();
		}
		else
		{
			glutSetWindow(janfmmtplan);
			glutHideWindow();
		}
		break;

	case 11:
	case 12:
	case 13:
	case 14:
	case 15:
	case 16:
	case 17:
	case 18:
	case 19:
	case 10: //comuta os graficos 
		vetoresKeyboard('0'+id-10, 0, 0);
		break;

	case 50:
		if(firstabout)
		{
			firstabout=false;
			constroiAbout();
		}
		showabout=!showabout;
		if(showabout)
		{
			glutSetWindow(janabout);
			glutShowWindow();
			glutPostRedisplay();
		}
		else
		{
			glutSetWindow(janabout);
			glutHideWindow();
		}
		break;

	case 100:
		exit(0);
		break;
	}
}


void controleRedisplay(void)
{
	if(showvetores)
	{
		interf->set_main_gfx_window(janvetores);
		glutSetWindow(janvetores);
		glutPostRedisplay();
	}
	if(showfmmtcir)
	{
		interf->set_main_gfx_window(janfmmtcir);
		glutSetWindow(janfmmtcir);
		glutPostRedisplay();
	}
	if(showfmmtplan)
	{
		interf->set_main_gfx_window(janfmmtplan);
		glutSetWindow(janfmmtplan);
		glutPostRedisplay();
	}
}

//void interfaceCreate(int argc, char** argv)
void interfaceCreate(void)
{
	//glutInit(&argc, argv);
	
	GLUI_Master.set_glutIdleFunc(calcSistema);
	
	glutInitWindowPosition (550,50);
	interf=GLUI_Master.create_glui(MSG67);

	titulo=interf->add_statictext(MSG68);
	titulo->set_alignment(GLUI_ALIGN_LEFT);
	
	spine=interf->add_spinner(MSG69, GLUI_SPINNER_INT, &localie, 1, changeControl);
	spine->set_int_limits(0, IEXC_MAX, GLUI_LIMIT_CLAMP);
	spine->set_speed(3);
	
	spinpt=interf->add_spinner(MSG70, GLUI_SPINNER_INT, &localpt, 2, changeControl);
	spinpt->set_int_limits(0, PT_MAX/1000, GLUI_LIMIT_CLAMP);
	spinpt->set_speed(3);
	
	interf->add_separator();

	opcoes=interf->add_rollout(MSG71,true);
	opcoes->set_w(170);
	vet=interf->add_button_to_panel(opcoes,MSG72, 5, pushControl);
	vet->set_w(150);
	dcir=interf->add_button_to_panel(opcoes,MSG73, 6, pushControl);
	dcir->set_w(150);
	dplan=interf->add_button_to_panel(opcoes,MSG74, 7, pushControl);
	dplan->set_w(150);
			
	sim=interf->add_rollout(MSG75, false);
	sim->set_w(170);
	cplay=interf->add_button_to_panel(sim,MSG76, 1, pushControl);
	cplay->set_w(150);
	splay=interf->add_button_to_panel(sim, MSG77, 2, pushControl);
	splay->set_w(150);
	stop=interf->add_button_to_panel(sim, MSG78, 3, pushControl);
	stop->set_w(150);

	graficos=interf->add_rollout(MSG79, false);
	graficos->set_w(170);
	b1=interf->add_button_to_panel(graficos, MSG80, 11, pushControl);
	b1->set_w(150);
	b2=interf->add_button_to_panel(graficos, MSG81, 12, pushControl);
	b2->set_w(150);
	b3=interf->add_button_to_panel(graficos, MSG82, 13, pushControl);
	b3->set_w(150);
	b4=interf->add_button_to_panel(graficos, MSG83, 14, pushControl);
	b4->set_w(150);
	b5=interf->add_button_to_panel(graficos, MSG84, 15, pushControl);
	b5->set_w(150);
	b6=interf->add_button_to_panel(graficos, MSG85, 16, pushControl);
	b6->set_w(150);
	b7=interf->add_button_to_panel(graficos, MSG86, 17, pushControl);
	b7->set_w(150);
	b8=interf->add_button_to_panel(graficos, MSG87, 18, pushControl);
	b8->set_w(150);
	b9=interf->add_button_to_panel(graficos, MSG88, 19, pushControl);
	b9->set_w(150);
	b0=interf->add_button_to_panel(graficos, MSG89, 10, pushControl);
	b0->set_w(150);

	interf->add_separator();
	about=interf->add_button(MSG89A,50, pushControl);
	about->set_w(170);
	
	quit=interf->add_button(MSG90, 100, pushControl);
	quit->set_w(170);

	glutMainLoop();
}


#endif
